package com.qs.home.ui.VM;

import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.ocr.demo.FileUtil;
import com.baidu.ocr.demo.MainActivity;
import com.baidu.ocr.demo.RecognizeService;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.ui.camera.CameraActivity;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.qs.base.global.SPKeyGlobal;
import com.qs.base.router.RouterActivityPath;
import com.qs.base.simple.zxing.ZXingActivity;
import com.qs.base.utils.BaseTimeUtils;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeHomeContentItemItemBinding;
import com.qs.home.entity.HomeContentEntity;

import java.util.Date;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 主页itemvm
 *
 * @Author ltzz
 * @Date 2019/8/8
 */
public class HomeItemViewModel extends ItemViewModel<HomeViewModel> {
    public ObservableField<HomeContentEntity> mItemEntity = new ObservableField<>();

    public HomeItemViewModel(@NonNull HomeViewModel viewModel, HomeContentEntity itemEntity) {
        super(viewModel);
        mItemEntity.set(itemEntity);
        if (StringUtils.isTrimEmpty(mItemEntity.get().getTitle())) {
            observableList.clear();
            if (mItemEntity.get().getButtons() != null && mItemEntity.get().getButtons().size() > 0) {
                for (int i = 0; i < mItemEntity.get().getButtons().size(); i++)
                    observableList.add(new HomeContentItemViewModel(viewModel, mItemEntity.get().getButtons().get(i)));
            }
        }
    }


    //给RecyclerView添加ObservableList
    public ObservableList<HomeContentItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<HomeContentItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<HomeContentItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, HomeContentItemViewModel item) {
            //设置布局
            itemBinding.set(BR.viewModel, R.layout.home_home_content_item_item);
        }
    });

    private Disposable subscribe;
    private String filePath = FileUtil.getSaveFile(viewModel.getApplication().getApplicationContext()).getAbsolutePath();
    //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象
    public BindingRecyclerViewAdapter<HomeContentItemViewModel> adapter = new BindingRecyclerViewAdapter<HomeContentItemViewModel>() {
        @Override
        public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, final HomeContentItemViewModel item) {
            super.onBindBinding(binding, variableId, layoutRes, position, item);
            HomeHomeContentItemItemBinding itemBinding = (HomeHomeContentItemItemBinding) binding;
            itemBinding.scanthingImg.setImageResource(item.mItemEntity.get().getBtIcon());
            itemBinding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (item.mItemEntity.get().getBtName()) {
                        case "拍照识别":
                            if (subscribe != null && !subscribe.isDisposed())
                                subscribe.dispose();
                            subscribe = RxBus.getDefault().toObservable(String.class)
                                    .subscribe(new Consumer<String>() {
                                        @Override
                                        public void accept(String result) throws Exception {
                                            KLog.e("拍照识别路径：" + result);
                                            viewModel.uc.photoDiscernUc.set(!viewModel.uc.photoDiscernUc.get());
                                            if (subscribe != null && !subscribe.isDisposed())
                                                subscribe.dispose();
                                        }
                                    });
                            RxSubscriptions.add(subscribe);
                            ARouter.getInstance().build(RouterActivityPath.OCR.CAMERA_ACTIVITY)
                                    .withString(CameraActivity.KEY_OUTPUT_FILE_PATH, filePath)
                                    .withString(CameraActivity.KEY_CONTENT_TYPE + "C", CameraActivity.CONTENT_TYPE_GENERAL)
                                    .navigation();
                            break;
                        case "图片识别":
                            viewModel.havePhoto = false;
                            viewModel.isMultiple = PictureConfig.MULTIPLE;
                            viewModel.uc.selectImg.set(!viewModel.uc.selectImg.get());

//                            if (subscribe != null && !subscribe.isDisposed())
//                                subscribe.dispose();
//                            subscribe = RxBus.getDefault().toObservable(String.class)
//                                    .subscribe(new Consumer<String>() {
//                                        @Override
//                                        public void accept(String result) throws Exception {
//                                            KLog.e("拍照识别路径：" + result);
//                                            viewModel.uc.photoDiscernUc.set(!viewModel.uc.photoDiscernUc.get());
//                                            if (subscribe != null && !subscribe.isDisposed())
//                                                subscribe.dispose();
//                                        }
//                                    });
//                            RxSubscriptions.add(subscribe);
//                            ARouter.getInstance().build(RouterActivityPath.OCR.CAMERA_ACTIVITY)
//                                    .withInt("type", 1)
//                                    .withString(CameraActivity.KEY_OUTPUT_FILE_PATH, filePath)
//                                    .withString(CameraActivity.KEY_CONTENT_TYPE+"C", CameraActivity.CONTENT_TYPE_GENERAL)
//                                    .navigation();
                            break;
                        case "证件扫描":
                            ARouter.getInstance().build(RouterActivityPath.Home.CERTIFICATE_RECOGNITION).navigation();
                            break;
                        case "票据扫描":
                            if (subscribe != null && !subscribe.isDisposed())
                                subscribe.dispose();
                            subscribe = RxBus.getDefault().toObservable(String.class)
                                    .subscribe(new Consumer<String>() {
                                        @Override
                                        public void accept(String result) throws Exception {
                                            KLog.e("票据识别路径：" + result);

                                            if (subscribe != null && !subscribe.isDisposed())
                                                subscribe.dispose();
                                            //是否会员
                                            boolean isOk = false;
                                            if (!StringUtils.isTrimEmpty(SPUtils.getInstance().getString(SPKeyGlobal.USER_ID))) {
                                                //用户已登录
                                                //查看是否是会员
                                                String date = SPUtils.getInstance().getString(SPKeyGlobal.USER_DATE, "");
                                                if (!StringUtils.isTrimEmpty(date)) {
                                                    if (Integer.parseInt(date) > 0) {
                                                        isOk = true;
                                                    }
                                                }
                                            }
                                            if (!isOk) {
                                                String seeNumberStr = SPUtils.getInstance().getString(SPKeyGlobal.SEE_NUMBER, "0");
                                                int x = Integer.parseInt(seeNumberStr);
                                                if (x <= 0) {
                                                    ToastUtils.showShort("识别次数已用完");
                                                    return;
                                                }
                                            }
                                            viewModel.uc.noteDiscernUc.set(!viewModel.uc.noteDiscernUc.get());
                                        }
                                    });
                            RxSubscriptions.add(subscribe);
                            ARouter.getInstance().build(RouterActivityPath.OCR.CAMERA_ACTIVITY)
                                    .withString(CameraActivity.KEY_OUTPUT_FILE_PATH, filePath)
                                    .withString(CameraActivity.KEY_CONTENT_TYPE + "C", CameraActivity.CONTENT_TYPE_GENERAL)
                                    .navigation();
                            break;
                        case "拍照翻译":
                            if (subscribe != null && !subscribe.isDisposed())
                                subscribe.dispose();
                            subscribe = RxBus.getDefault().toObservable(String.class)
                                    .subscribe(new Consumer<String>() {
                                        @Override
                                        public void accept(String result) throws Exception {
                                            KLog.e("拍照翻译路径：" + result);
                                            viewModel.uc.photoDiscernUc.set(!viewModel.uc.photoDiscernUc.get());
                                            if (subscribe != null && !subscribe.isDisposed())
                                                subscribe.dispose();
                                        }
                                    });
                            RxSubscriptions.add(subscribe);
                            ARouter.getInstance().build(RouterActivityPath.OCR.CAMERA_ACTIVITY)
                                    .withString(CameraActivity.KEY_OUTPUT_FILE_PATH, filePath)
                                    .withString(CameraActivity.KEY_CONTENT_TYPE + "C", CameraActivity.CONTENT_TYPE_GENERAL)
                                    .navigation();
                            break;
                        case "图片翻译":
                            viewModel.havePhoto = false;
                            viewModel.isMultiple = PictureConfig.MULTIPLE;
                            viewModel.uc.selectImg.set(!viewModel.uc.selectImg.get());
                            break;
                        case "阅读翻译":
                            viewModel.havePhoto = true;
                            viewModel.isMultiple = PictureConfig.MULTIPLE;
                            viewModel.uc.selectImg.set(!viewModel.uc.selectImg.get());
                            break;
                        case "文字翻译":
                            viewModel.havePhoto = false;
                            viewModel.isMultiple = PictureConfig.MULTIPLE;
                            viewModel.uc.selectImg.set(!viewModel.uc.selectImg.get());
                            break;
                        case "二维码扫描":
                            if (subscribe != null && !subscribe.isDisposed())
                                subscribe.dispose();
                            subscribe = RxBus.getDefault().toObservable(String.class)
                                    .subscribe(new Consumer<String>() {
                                        @Override
                                        public void accept(String result) throws Exception {
                                            KLog.e("二维码扫描：" + result);
                                            ARouter.getInstance().build(RouterActivityPath.Home.QR_RESULT_ACTIVITY).withString("qrResult", result).navigation();
                                            if (subscribe != null && !subscribe.isDisposed())
                                                subscribe.dispose();
                                        }
                                    });
                            RxSubscriptions.add(subscribe);
                            ARouter.getInstance().build(RouterActivityPath.Base.ZXING_ACTIVITY).navigation();
                            break;
                        case "二维码生成":
                            ARouter.getInstance().build(RouterActivityPath.Home.QR_GENERATE_ACTIVITY).navigation();
                            break;
                    }
                }
            });

        }
    };

}
